<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>预分配标记值管理</title>
		<s:head />
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/systemPreassignTypeAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/forCSS.js'></script>
		<script type="text/javascript">
		var  totalcount; //全局变量，总行数
		var preassignCache = {};
		//查询总行数
			function searchTotalCount(){
				systemPreassignTypeAction.getAllPreassignTypeRows(tcbck);
			}
			
			var tcbck = function tc_bck(bckcount){
				totalcount = bckcount; // 给总行数赋值
				systemPreassignTypeAction.listAllPreassignTypeByPage(1,bckcount,renderdormcallback);
			}
			
			var renderdormcallback  = function renderdormcb(preidlist){
			if($('pageTable').style.visibility == 'hidden'){
				$('pageTable').style.visibility = 'visible';
			}
			if($('listAlloInf').style.visibility == 'hidden'){
				$('listAlloInf').style.visibility = 'visible';
			}
			if($('addalloinf').style.visibility == 'visible'){
				$('addalloinf').style.visibility = 'hidden';
			}
			if($('allinfodiv').style.visibility == 'visible'){
				$('allinfodiv').style.visibility = 'hidden';
			}
				
		DWRUtil.removeAllRows('ttb', { filter:function(tr) {
		      return (tr.id != "pattern");
			    }});
		var Alloinfo,id;
		 for (var i =0; i < preidlist.length ; i++) {
			  Alloinfo = preidlist[i];
			  id = Alloinfo.allocationid;
			  //alert(id);
			  dwr.util.cloneNode("pattern", { idSuffix:id });
		     dwr.util.setValue("tableid"+id, Alloinfo.allocationid);//隐藏项
		     dwr.util.setValue("tablename"+id, Alloinfo.allocationname);
		    // Alloinfo.starttime;
		    // var date = new Date();
		    // date.
		  	// alert( date += Alloinfo.starttime.date);
		    
		    // Date sdate = Alloinfo.starttime;
		  	var sdate = new Date();
		  	if(Alloinfo.starttime != null){
			  	sdate.setTime( Alloinfo.starttime.time);
			  	dwr.util.setValue("tablestarttime"+id,sdate.getFullYear()+"-"+eval(sdate.getMonth()+1)+"-"+sdate.getDate());
		    }
		    var edate = new Date();
		    if(Alloinfo.endtime!=null){
		    	edate.setTime(Alloinfo.endtime.time);
			  	dwr.util.setValue("tableendtime"+id,edate.getFullYear()+"-"+eval(edate.getMonth()+1)+"-"+edate.getDate());
			}   
			dwr.util.setValue("cbx" + id, Alloinfo.allocationid);
		 	$("pattern" + id).style.display= "";
		 	preassignCache[id] = Alloinfo;
		   }
		   	setPageInfo();
		   	tbCSS();
			}
			
	function setPageInfo(){
        $('pageTd').innerHTML = '';
        systemPreassignTypeAction.getPage({    
            callback:function(data){
                page = data;
                if(page.totalPage != 0){
                    var pageSpan = document.createElement('span');
                    pageSpan.innerHTML = '共 '+totalcount+' 条 '+'   第 '+page.currentPage+' 页    共'+page.totalPage+' 页 ';
                    $('pageTd').appendChild(pageSpan);
                    var aFirst = document.createElement('a');
                    aFirst.setAttribute('href','javascript:getFirstData()');
                    aFirst.innerHTML = '首页';
                    $('pageTd').appendChild(aFirst);
                    var aPrevious = document.createElement('a');
                    aPrevious.setAttribute('href','javascript:getPreviousData()');
                    aPrevious.innerHTML = '上一页 ';
                    $('pageTd').appendChild(aPrevious);
                    var aNext = document.createElement('a');
                    aNext.setAttribute('href','javascript:getNextData()');
                    aNext.innerHTML = '下一页 ';
                    $('pageTd').appendChild(aNext);
                    var aLast = document.createElement('a');
                    aLast.setAttribute('href','javascript:getLastData()');
                    aLast.innerHTML = '尾页';
                    $('pageTd').appendChild(aLast);   
                }              
            }
        });
        $('loading').style.display = 'none';
    } 
		function getFirstData(){
			page.currentPage = 1;
			getDataPerPage();
		}
		
		 function getPreviousData(){
        if(page.currentPage>1){
            page.currentPage--;
        }
        getDataPerPage();
  	  }
	    
	   	 function getNextData(){
	        if(page.currentPage<page.totalPage){
	            page.currentPage++;
	        }
	        getDataPerPage();
	    }
		
		function getLastData(){
			page.currentPage = page.totalPage;
			getDataPerPage();
		}
		
		
		function getDataPerPage(){
			$('loading').style.display = 'block';
			systemPreassignTypeAction.listAllPreassignTypeByPage(page.currentPage,totalcount,renderdormcallback);
		}	
		
		
		function editClicked(eleid) {
		if($('allinfodiv').style.visibility == 'hidden'){
			$('allinfodiv').style.visibility = 'visible';
		}
		  //得到的eleid为"edit{id}"，如 "edit42".,经过substring后为42,person的id
		  eid = eleid.substring(4);
		  var allo_info = preassignCache[eid];  
		   dwr.util.setValues(allo_info); 
		   
		  var editstarttime = new Date();
		  if(allo_info.starttime != null){
			  editstarttime.setTime(allo_info.starttime.time);
			  dojo.widget.byId("editstarttime").setValue(getCommonDate(editstarttime));
		  }
		  var editendtime = new Date();
		    if(allo_info.endtime != null){
				editendtime.setTime(allo_info.endtime.time);
				var sdfsdfs = getCommonDate(editendtime);
				dojo.widget.byId("editendtime").setValue(sdfsdfs);
		 	}
		}
		
		//传入date类型日期获得xxxx-xx-xx字符串
		function getCommonDate(thedate){
			
			var mth = eval(thedate.getMonth()+1);
			if(mth < 10){
				mth = "0"+mth;
				//alert(mth+"@")
			}
			var dt = thedate.getDate();
			if(dt < 10){
				dt = "0"+dt;
			}
			return thedate.getFullYear()+"-"+mth+"-"+dt;
		
		}
		function delClicked(eleid){
		//得到的eleid为"mydelete{id}"
		var alloid = eleid.substring(8);
		systemPreassignTypeAction.deleltePreassignTypeByID(alloid,delcb)
		}
		var delcb = function del_cb(stas){
			alert(stas);
			searchTotalCount();
		}
		
	   function writeAlloInfo(){
		var alloi = { allocationid:null, allocationname:null };
		dwr.util.getValues(alloi);
		var ay = new Array(alloi.allocationid,alloi.allocationname);
		  if(alloi.allocationname==""){
				alert("请填写预分配名");
				return;
			  }
		  systemPreassignTypeAction.updatePreassignType(ay+"",function(status){
		  alert(status);
		  searchTotalCount();
		  });
		}
		
		function clearAlloInfo() { //id没有更新,相当于清除充填
		  dwr.util.setValues({allocationid:null, allocationname:null });
		}
		
		//添加预分配标记值
		function showaddpreiddiv(){
			if($('listAlloInf').style.visibility == 'visible'){
					$('listAlloInf').style.visibility = 'hidden';
				}
			if($('addalloinf').style.visibility == 'hidden'){
				$('addalloinf').style.visibility = 'visible';
			}
			if($('pageTable').style.visibility == 'visible'){
				$('pageTable').style.visibility = 'hidden';
			}
			if($('allinfodiv').style.visibility == 'visible'){
				$('allinfodiv').style.visibility = 'hidden';
			}
		}
		function addpreid(){
			var aiinfo = $("aiinfo").value;
			if(aiinfo==""){
				alert("请输入要增加的预分配标记值");
				return false;
			}
			var startindate= dojo.widget.byId("startInDate").getValue();
			var endoutdate= dojo.widget.byId("endOutDate").getValue();
			systemPreassignTypeAction.addPreassignType(aiinfo,startindate,endoutdate,addcb);
		}
		var addcb = function add_cb(status){
		alert(status);
		searchTotalCount();
		}
		
		</script>
	</head>
	<body>
		<div id="page_container">
			<%@   include file="banner.jsp"%>
			<div id="outlier">
			</div>
			<input class="long_button" type="button" value="获取预分配值"
				onclick="javascript:searchTotalCount();">
			<input class="long_button" type="button" value="添加预分配值"
				onclick="javascript:showaddpreiddiv();">
			<div id="listAlloInf" style="visibility: hidden;">
				<table class="table_common table_list_style">
					<thead>
						<tr>
							<th width="6%" height="26">
								<div>
									选择
								</div>
							</th>
							<th width="24%" height="18">
								<div>
									预分配值
								</div>
							</th>
							<th width="24%" height="18">
								<div>
									开始时间
								</div>
							</th>
							<th width="24%" height="18">
								<div>
									结束时间
								</div>
							</th>
							<th width="12%">
								<div>
									编辑
								</div>
							</th>
							<th width="7%" height="18">
								<div>
									删除
								</div>
							</th>
						</tr>
					</thead>
					<tbody id="ttb">
						<tr id="pattern" style="display: none;">
							<td>
								<div>
									<input type="checkbox" id="cbx" name="cbx" value="checkbox"
										onclick="test(this.id)" />
								</div>
							</td>
							<!-- 隐藏的项目 -->
							<div id="tableid" style="display: none"></div>
							<td>
								<div id="tablename">
								</div>
							</td>
							<td>
								<div id="tablestarttime">
								</div>
							</td>
							<td>
								<div id="tableendtime">
								</div>
							</td>
							
							<td>
								<div>
									<a href="#" id="edit" onclick="javascript:editClicked(this.id)"><img
											src="../images/037.gif" width="9" height="9" /> </a>
								</div>
							</td>
							<td>
								<div>
									<a href="#" id="mydelelt"
										onclick="javascript:if(confirm('确认删除！')) delClicked(this.id)">
										<img src="../images/010.gif" width="9" height="9" /> </a>
								</div>
							</td>
						</tr>
					</tbody>
				</table>
				<table id="pageTable" align="center"
					style="visibility: hidden; margin-top: 30px; font: 12px;"
					class="navigator">
					<tr>
						<td id="pageTd">
						</td>
					</tr>
				</table>
				<div id="loading"
					style="display: none; margin-top: 20px; text-align: center; color: red; font: 13px;">
					Loading...
				</div>
			</div>

			<!-- 添加预分配值div开始 -->
			<div id="addalloinf" style="visibility: hidden;">
				<div id="startt">
					开始入住时间：
				<s:datetimepicker name="startInDate" id="startInDate"
					value="today" displayFormat="yyyy-MM-dd" />
				</div>
	
				<div id="endt">
					结束入住时间：
					<s:datetimepicker name="endOutDate" id="endOutDate"
						displayFormat="yyyy-MM-dd" />
				</div>
				请输入要增加的预分配标记值：
				<input type="text" id="aiinfo" />
				<input class="common_button" type="submit" value="新增"
					onclick="javascript:addpreid()" />
			</div>

			<!-- 编辑预分配标记开始-->
			<div id="allinfodiv" style="visibility: hidden;">

				<table>
					<tr>
						<td>
							<input id="allocationid" type="hidden" />
						</td>
					</tr>
					<tr>
						<td>
							<div >
								开始入住时间：
							<s:datetimepicker name="editstarttime" id="editstarttime"
								  displayFormat="yyyy-MM-dd" />
							</div>
						</td>
					</tr>
					<tr>
						<td>
							<div >
								结束入住时间：
								<s:datetimepicker name="editendtime" id="editendtime"
									displayFormat="yyyy-MM-dd" />
							</div>
						</td>
					</tr>
					<tr>
						<td>
							预分配名
						</td>
						<td>
							<input id="allocationname" type="text" />
						</td>
					</tr>
					<tr>
						<td colspan="2" align="right">
							<input class="common_button" type="button" value="保存"
								onclick="writeAlloInfo()" />
							<input class="common_button" type="button" value="清除"
								onclick="clearAlloInfo()" />
						</td>
					</tr>
				</table>


			</div>
			<!-- 编辑预分配标记结束 -->

		</div>
	</body>
</html>